package com.thysdy.flowable.controller;

import com.thysdy.flowable.entity.PageEntity;
import com.thysdy.flowable.entity.ProcessInstanceQueryVo;
import com.thysdy.flowable.entity.ProcessInstanceVo;
import com.thysdy.flowable.entity.RequestVo;
import com.thysdy.flowable.enums.CodeEnum;
import com.thysdy.flowable.exception.MyException;
import com.thysdy.flowable.service.InstanceService;
import com.thysdy.flowable.vo.ReturnVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class InstanceController {
    @Autowired
    InstanceService repairInstanceService;

    /**
     * 挂起或激活流程实例
     *
     * @param request
     * @return
     */
    @PostMapping("/saProcessInstanceById")
    public ReturnVo saProcessInstanceById(@RequestBody RequestVo request) {
        try {
            repairInstanceService.suspendOrActivateProcessInstanceById(request.getIds(), request.getSuspensionState());
            return ReturnVo.success();
        } catch (MyException e) {
            return ReturnVo.exception(e);
        }
    }

    /**
     * 彻底删除流程实例
     *
     * @param requestVo
     * @return
     */
    @DeleteMapping("/deleteInstanceCompletely")
    public ReturnVo deleteInstanceCompletely(@RequestBody RequestVo requestVo) {
        try {
            repairInstanceService.deleteInstanceCompletely(requestVo.getIds());
            return ReturnVo.success();
        } catch (Exception e) {
            return ReturnVo.exception(new MyException(CodeEnum.commonException));
        }
    }

    /**
     * 查询流程实例
     *
     * @param queryVo
     * @return
     */
    @PostMapping("/getFlowableInstances")
    public ReturnVo getFlowableInstances(@RequestBody ProcessInstanceQueryVo queryVo) {

        try {
            PageEntity pageEntity = repairInstanceService.getFlowableInstances(queryVo);
            return ReturnVo.success(pageEntity);
        } catch (MyException e) {
            return ReturnVo.exception(e);
        }
    }

    /**
     * 根据业务主键查询流程实例
     *
     * @param requestVo
     * @return
     */
    @PostMapping("/getFlowableInstancesByBusinessAndDefinitionKey")
    public ReturnVo getFlowableInstancesByBusinessAndDefinitionKey(@RequestBody RequestVo requestVo) {

        try {
            List<ProcessInstanceVo> pageEntity = repairInstanceService.getFlowableInstancesByBusinessAndDefinitionKey(requestVo);
            return ReturnVo.success(pageEntity);
        } catch (MyException e) {
            return ReturnVo.exception(e);
        }



    }

    /**
     * 根据业务主键查询最新流程实例
     *
     * @param requestVo
     * @return
     */
    @PostMapping("/getNewestFlowableInstancesByBusinessAndDefinitionKey")
    public ReturnVo getNewestFlowableInstancesByBusinessAndDefinitionKey(@RequestBody RequestVo requestVo) {
        try {
            ProcessInstanceVo instance = repairInstanceService.getNewestFlowableInstancesByBusinessAndDefinitionKey(requestVo);
            return ReturnVo.success(instance);
        } catch (MyException e) {
            return ReturnVo.exception(e);
        }
    }

    /**
     * 撤回流程实例(删除)
     *
     * @param requestVo
     * @return
     */
    @PostMapping("/revokeProcessInstance")
    public ReturnVo revokeProcessInstance(@RequestBody RequestVo requestVo) {
        try {
            repairInstanceService.revokeProcessInstance(requestVo);
            return ReturnVo.success();
        } catch (MyException e) {
            return ReturnVo.exception(e);
        }
    }

}
